import json

from flask import Blueprint, request

# 设备分组
import sql

equipmentGroup = Blueprint('EquipmentGroupController', __name__)


# 获取所有分组信息
@equipmentGroup.route('/getEquipmentGroup')
def getEquipmentGroup():
    return '设备分组，拿到了'


# 添加设备分组信息
@equipmentGroup.route('/addEquipmentGroup', methods=['GET', 'POST'])
def addEquipmentGroup():
    req = json.loads(request.data)
    group_name = req["group_name"]
    group_devices_list = req["group_devices_list"]
    res = sql.add_group(group_name, group_devices_list)
    if res > 0:
        print('add group %s success' % group_name)
        return 'success'
    else:
        return 'Fail.This name already exists in database.'


# 修改设备分组信息
@equipmentGroup.route('/updateEquipmentGroup', methods=['GET', 'POST'])
def updateEquipmentGroup():
    req = json.loads(request.data)
    group_id = req["group_id"]
    group_name = req["group_name"]
    group_devices_list = req["group_devices_list"]
    res = sql.update_group(group_id,group_name, group_devices_list)
    if res > 0:
        print('update group %s success' % group_name)
        return 'success'
    else:
        return 'Fail.This name already exists in database.'


# 删除设备分组信息
@equipmentGroup.route('/deleteEquipmentGroup', methods=['GET', 'POST'])
def deleteEquipmentGroup():
    req = json.loads(request.data)
    group_id = req['group_id']
    res = sql.delete_group(group_id)

    if res > 0:
        print('delete group %s success' % group_id)
        return 'success'
    else:
        return 'Fail.This name already exists in database.'
